home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: uu4news.netcom.com!zodiac!szh
- From: szh@zcon.com (Syed Zaeem Hosain)
- Subject: Re: Ability to locate spaces?
- Message-ID: <1996Feb16.162842.18380@zcon.com>
- Sender: szh@zcon.com (Syed Zaeem Hosain)
- Nntp-Posting-Host: zodiac
- Reply-To: szh@zcon.com
- Organization: Z Consulting Group
- References: <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu>
- Date: Fri, 16 Feb 1996 16:28:42 GMT
-
- In article <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu>, John Young Oh <jyo@acpub.duke.edu> writes:
- >I am trying to write a program that reads an input file and locates where
- >the spaces are located. For example, if a line from the input file read:
- >555555.55 5555.55 5555.55
- >
- >I would like the program to be able to recognize that there are spaces
- >between the numbers and keep track of them. What I did was to use
- >fgets and read in the line into a string. I then used a for loop
- >and checked each array element of the character string and used an
- >if statement to see if an array element was a space.
- >Here are the lines of code:
- >
- >FILE *input;
- >char *string, buf[200];
- >int i, count;
- >input = fopen ("data", "r");
- >(etc etc etc)
- >string = fgets (buf, 199, input);
- >for (i = 0; i < 40; ++i)
- >{
- > if (buf[i] == ' ')
- > {
- > ++count;
- > }
- >}
- >
- >What is wrong with this code? It does not seem to work.
-
- Well, what did the program do? What is the error? Is it a compiler
- problem or a run-time problems? "Does not seem to work" is pretty darn
- vague, in other words! :-)
-
- Here is what I would check:
-
- 1. Did you initialize count to 0 before the lines get read?
-
- 2. Did you check to see if fgets returns a legit result? I.e. the line
- has something to read?
-
- 3. Is the line longer than 40 characters? If so, why does your loop
- stop at 40 characters? Why not use the `\n` byte as a terminator for
- the loop? Or the '\0' byte for that matter?
-
- 4. Are the lines longer than 199 characters? If so, why is your "buf"
- size set to 200?
-
- 5. Did the call to "fopen" succeed? Are you reading real lines from
- the input program?
-
- 6. Is the filename correct? Does the file called "data" exist?
-
- There may be other issues, but these are the ones that come to mind off
- the top of my head since I do not know the symptoms you are actually
- seeing when running the program.
-
- As an aside: your first sentence says "... locates where the spaces are
- located". Later you say "recognize that there are spaces ... and keep
- track of them". What are you really trying to do - simply count them or
- something else?
-
- Z
-
-
- --
- -------------------------------------------------------------------------
- | Syed Zaeem Hosain P. O. Box 610097 (408) 441-7021 |
- | Z Consulting Group San Jose, CA 95161 szh@zcon.com |
- -------------------------------------------------------------------------
-